package com.rpframework.website.device.service;

import java.util.List;

import javax.annotation.Resource;

import org.springframework.stereotype.Service;
import org.springframework.util.Assert;

import com.rpframework.core.BaseService;
import com.rpframework.utils.NumberUtils;
import com.rpframework.utils.Pager;
import com.rpframework.website.device.dao.ITCommentDao;
import com.rpframework.website.device.domain.TComment;
import com.rpframework.website.device.domain.Praise;
import com.rpframework.website.device.domain.TComment;
import com.rpframework.website.device.util.HConstants;

@Service
public class TCommentService extends BaseService{
	public @Resource ITCommentDao iDao;
	
	public Pager<TComment> getPager(Pager<TComment> pager) {
		long startTime = System.currentTimeMillis();
		List<TComment> list = iDao.doPager(this.packageMyBatisParam(pager));
		pager.setItemList(list);
		pager.setCostTime(System.currentTimeMillis() - startTime);
		return pager;
	}

	public boolean doSaveOrUpdate(Praise t) {
		t.setCreateTime(System.currentTimeMillis()/1000);
		return iDao.insert(t);
	}

	public boolean addComment(Integer relativeId, Integer type, Integer userId,String content,String urlList) {
		TComment t = new TComment();
		t.setContent(content);
		t.setUrlList(urlList);
		t.setRelativeId(relativeId);
		t.setUserId(userId);
		t.setState(1);
		t.setType(type);
		t.setCreateTime(System.currentTimeMillis()/1000);
		return iDao.insert(t);
	}

	public Integer queryAll(Integer id, Integer type) {
		Integer i = 0;
		i = iDao.queryAll(id,type);
		return i==null ? 0 : i;
	}

	public boolean doSaveOrUpdate(TComment t) {
		if(t == null) {
			throw new IllegalArgumentException("非法参数!");
		}
		if(NumberUtils.isValid(t.getId())) {
			TComment DBUtils = iDao.select(t.getId());
			Assert.notNull(DBUtils, "更新时找不到实体 " + t.getId());
			if(t.getUrlList()==null){
				t.setUrlList(DBUtils.getUrlList());
			}
			t.setModifyTime(System.currentTimeMillis() / 1000);
			return iDao.update(t);
		}
		t.setCreateTime(System.currentTimeMillis() / 1000);
		return iDao.insert(t);
	}

	public boolean doUpdateState(Integer id) {
		if(NumberUtils.isValid(id)){
			TComment t = iDao.select(id);
			if(t.getState() == HConstants.EntityState.STATE_NORMAL){
				t.setState(HConstants.EntityState.STATE_DISABLE);
			}else{
				t.setState(HConstants.EntityState.STATE_NORMAL);
			}
			t.setModifyTime(System.currentTimeMillis()/1000);
			return iDao.update(t);
		}
		return false;
	}


}
